package com.wh.springboot.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import com.wh.springboot.common.Result;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.wh.springboot.service.IXueYuanService;
import com.wh.springboot.common.entity.XueYuan;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author elsy
 * @since 2023-04-17
 */
@RestController
@RequestMapping("/xueyuan")
public class XueYuanController {
    @Resource
    private IXueYuanService xueYuanService;

    // 新增和修改
    @PostMapping
    public Result save(@RequestBody XueYuan xueYuan) {
        //新增或者更新
        return Result.success(xueYuanService.saveOrUpdate(xueYuan));
    }

    //  查询所有数据
    @GetMapping
    public Result findAll() {
        return Result.success(xueYuanService.list());
    }

    //根据id查询
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(xueYuanService.getById(id));
    }

    //删除数据
    @DeleteMapping("/{id}")//id 与下面的id必须一致
    //    @PathVariable 表示 url 这个参数
    public Result delete(@PathVariable Integer id) {
        return Result.success(xueYuanService.removeById(id));
    }

    //批量删除
    @DeleteMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(xueYuanService.removeByIds(ids));
    }

    //分页查询
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String name) {
        QueryWrapper<XueYuan> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
        return Result.success(xueYuanService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}

